package com.sangmo.fts.clazz.service;

import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.clazz.model.Clazz;
import com.sangmo.fts.clazz.model.UserClazz;
import com.sangmo.fts.clazz.repo.UserClazzRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserClazzEntityService extends BaseViewService<UserClazz> {

    @Autowired
    private UserClazzRepo userClazzRepo;

    @Override
    protected IDataViewRepo<UserClazz> getRepo() {
        return userClazzRepo;
    }

    public List<UserClazz> findByUserId(Long userId) {
        return userClazzRepo.findByUserId(userId);
    }

    public UserClazz createIfNotExists(Long userId, Long clazzId) {
        UserClazz entity = userClazzRepo.findByUserIdAndClazzId(userId, clazzId);
        if (null == entity) {
            UserClazz userClazz = new UserClazz();
            userClazz.setUserId(userId);
            userClazz.setClazzId(clazzId);
            return this.create(userClazz);
        }
        return entity;
    }
}
